/*
 * @Author: chenhuanhuan 2925102113@qq.com
 * @Date: 2024-12-05 10:10:43
 * @LastEditors: wuheng wuheng@szhbzc.com
 * @LastEditTime: 2025-02-17 11:35:10
 * @FilePath: \hbzc-groupmeal-restaurant-web\src\Public-UIlibrary\pb-message-box\method.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { render, h } from "vue";
import { PayLoadType, } from "./types";
import confirmComponent from "./index.vue";

export const hua5MsgBox = (content: string, payLoad: PayLoadType = {}) => {
  const { type = 'info', title, cancelText, confirmText, Width, connectList, showIcon, titleType, showTitleIcon, showConfirm } = payLoad
  return new Promise((resolve, reject) => {
    // 取消按钮事件
    const cancelHandler = () => {
      reject(false)
    }
    // 确定按钮事件
    const confirmHandler = () => {
      resolve(true)
    }
    // 关闭弹层事件
    const closeBox = () => {
      render(null, document.body)
    }

    // 1. 生成 vnode
    const vnode = h(confirmComponent, {
      content,
      type,
      title,
      showConfirm,
      cancelText,
      confirmText,
      Width,
      connectList,
      showIcon,
      showTitleIcon,
      titleType,
      cancelHandler,
      confirmHandler,
      closeBox,

    })
    // 2. render 渲染
    render(vnode, document.body)
  })
}

export const to = (promise: Promise<any>): Promise<any> => {
  return promise.then(res => res).catch(error => error)
}

export const createMsgBox = (content: string, payLoad?: PayLoadType) => {
  return to(hua5MsgBox(content, payLoad))
}

